<?php
namespace app\index\service;


use app\index\model\MessageModel;
use think\Exception;
use think\facade\Config;

class MessageService
{


    public function send_reg($tel)
    {
        $template = Config::get('service.sms.reg');
        $code = rand(100000, 999999);
        $msg = ['code' => $code];
        try{
            $sendInfo =  send_message($template, $tel, $msg);
            if ($sendInfo['code'] == 'OK') {
                MessageModel::add_message( $tel, $code);
            }
        }catch (\Exception $e){
            throw new Exception('发送失败');
        }
        return true;
    }


    public function check_code($tel, $code){
        $where[] = ['tel','=',$tel];
        $where[] = ['code','=',$code];
        $where[] = ['status','=',1];
        $where[] = ['expirydt','gt',date('Y-m-d H:i:s')];
        $messageInfo = MessageModel::where($where)->order('id desc')->find();
        if (!$messageInfo) {
            return false;
        }
        MessageModel::update(['status' => 2],['id' =>  $messageInfo['id']]);
        return true;
    }


}
